<?php

namespace Our;

use Yaf\Registry;

/**
 * 控制器抽象类
 */
abstract class Controller_Abstract extends \Yaf\Controller_Abstract
{
    /**
     * 输出错误的信息
     *
     * @param string $message
     * @param int $errno 错误编号
     */
    protected function _echoErrorMessage($message, $errno = 1)
    {
        $return = array(
            'status' => $errno . "",
            'message' => $message . "",
            'data' => array()
        );
        if ($this->getRequest()->getQuery('is_jsonp')) {
            $callback = $this->getRequest()->getQuery('callback');
            if ($callback) {
                $this->getResponse()->setBody($callback . "(" . json_encode($return) . ")");
            }
        } else {
            $this->_displayJson($return);
        }
    }

    /**
     * 请求时输出成功的信息,并退出
     *
     * @param string $message
     * @param array $data
     */
    protected function _echoSuccessMessage($message, $data = array())
    {
        //检测fields字段，如果有设置则只传输相应的字段值
        $fields = array_filter(explode(',', $this->getRequest()->getParam('fields')));
        if ($fields) {
            foreach ($data as $k => $v) {
                if (!in_array($k, $fields)) {
                    unset($data[$k]);
                }
            }
        }

        $return = array(
            'status' => "0",
            'message' => $message,
            'data' => $data
        );

        //检测是否有jsonp的
        if ($this->getRequest()->getQuery('is_jsonp')) {
            $callback = $this->getRequest()->getQuery('callback');
            if ($callback) {
                $this->getResponse()->setBody($callback . "(" . json_encode($return) . ")");
            }
        } else {
            $this->_displayJson($return);
        }
    }

    /**
     * 输出json格式的数据
     *
     * @param mixed $data
     */
    public function _displayJson($data)
    {
        \Yaf\Dispatcher::getInstance()->disableView();
        if (!$this->getRequest()->isCli()) {
            header('Content-Type: application/json; charset=utf-8');
        }
        $this->getResponse()->setBody(json_encode($data));
    }

    /**
     * 获取post的参数
     *
     * @param string $key
     * @param mixed $defaultValue
     * @return mixed
     */
    protected function _getPost($key = null, $defaultValue = null)
    {
        if (!$key) {
            return $_POST;
        }

        $value = $this->getRequest()->getPost($key);
        if (!$value) {
            $value = isset($_POST[$key]) ? $_POST[$key] : null;
        }
        if (!$value) {
            $value = $defaultValue;
        }
        return $value;
    }

    /**
     * 获取get参数
     *
     * @param type $key
     * @param type $defaultValue
     * @return type
     */
    protected function _getParam($key = null, $defaultValue = null)
    {
        if (!$key) {
            $params = $this->getRequest()->getParams();
            if ($defaultValue) {
                foreach ($defaultValue as $k => $v) {
                    if (empty($params[$k])) {
                        $params[$k] = $v;
                    }
                }
            }

            return $params;
        }

        return $this->getRequest()->getParam($key, $defaultValue);
    }

    /**
     * 获取get请求参数的值
     * @param string $key
     * @param mixed $defaultValue
     * @return mixed
     */
    protected function _getQuery($key = null, $defaultValue = null)
    {
        if (!$key) {
            $params = $this->getRequest()->getQuery();
            if ($defaultValue) {
                foreach ($defaultValue as $k => $v) {
                    if (empty($params[$k])) {
                        $params[$k] = $v;
                    }
                }
            }

            return $params;
        }

        return $this->getRequest()->getQuery($key, $defaultValue);
    }

    /**
     * 检测是否是post请求
     *
     * @return boolean
     */
    protected function _isGet()
    {
        return $this->getRequest()->isGet();
    }

    /**
     * 检测是否是post请求
     *
     * @return boolean
     */
    protected function _isPost()
    {
        return $this->getRequest()->isPost();
    }

    /**
     * 将值传递到模板
     *
     * @param string $name
     * @param mixed $value
     */
    protected function _assign($name, $value)
    {
        $this->getView()->assign($name, $value);
    }

    /**
     * 将值传递到模板
     *
     * @param string $tpl
     * @param array $value
     */
    protected function _display($tpl, $value = array())
    {
        \Yaf\Dispatcher::getInstance()->disableView();
        $this->getView()->display($tpl, $value);
    }

    /**
     * 将值传递到模板
     *
     * @param string $tpl
     * @param array $value
     */
    protected function _render($tpl, $value = array())
    {
        \Yaf\Dispatcher::getInstance()->disableView();
        return $this->getView()->render($tpl, $value);
    }

    /**
     * 重定向
     *
     * @param string $url
     * @return boolean
     */
    public function redirect($url)
    {
        if (strpos($url, 'http://') === false) {
            $host = Registry::get('config')->get('application.host');
            $url = rtrim($host, '/') . '/' . ltrim($url, '/');
        }
        return parent::redirect($url);
    }

}
